package com.dtm.ui;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.dtm.R;
import com.dtm.adapter.AdapterUser;
import com.dtm.adapter.ServerAdapter;
import com.dtm.client.ClientConnectedThread;
import com.dtm.client.thread.OnServerChangeListener;
import com.dtm.client.thread.SearchHostThread;
import com.dtm.model.User;

public class SearchHost extends Activity implements OnItemClickListener, OnServerChangeListener{
	private ServerAdapter serverAdapter;
	
	private ListView listServer;
	
	private Handler handler = new Handler();
	private ArrayList<User> arrHost = new ArrayList<User>();
	private AdapterUser adapter = null;
//	private AtomicBoolean abool = new AtomicBoolean(false);
	private Button btn_refresh;
	
	public static String ipAdress = "";
	public static int port;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_host);
		
		listServer = (ListView) findViewById(R.id.list_server_new);
		serverAdapter = new ServerAdapter(getApplicationContext(), -1, new ArrayList<ServiceInfo>());
		listServer.setAdapter(serverAdapter);
		listServer.setOnItemClickListener(this);
		
		SearchHostThread threadClient = new SearchHostThread(getApplicationContext(), SearchHost.this, handler);
		new Thread(threadClient).start();
		

	}

	private void updateUI(User host){
		arrHost.add(host);
		adapter.notifyDataSetChanged();
	}
	
	private void finishUpdate(){
		Toast.makeText(this, "Finish", 1000).show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search_host, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		ServiceInfo serviceInfo = serverAdapter.getItem(position);
		port = serviceInfo.getPort();
		Log.e("Search Host", "" + port);
		String tempAdress = serviceInfo.getInet4Addresses()[0].toString();
		int length = serviceInfo.getInet4Addresses()[0].toString().length();
		for (int i = 1; i < length; i++) {
			ipAdress = ipAdress + tempAdress.charAt(i);
		}
		Log.e("Search Host", ipAdress);
		Intent intent = new Intent(SearchHost.this, InRoomActivity.class);
		intent.putExtra("key", MainActivity.CLIENT_ACTIVITY);
		startActivity(intent);
	}

	@Override
	public void onFound(ServiceEvent ev) {
		serverAdapter.add(ev.getInfo());
		serverAdapter.notifyDataSetChanged();
	}

	@Override
	public void onRemove(ServiceEvent ev) {
		// TODO Auto-generated method stub
		
	}

}
